<?php
namespace app\home\controller;
use think\Controller;

class Shop extends Controller
{

    public  function  _initialize()
    {

    }
  //向购物车里添加数据(id问题暂定，不确定返回的数据是不是ID号，并且不知道$re的值是不是常在)
  public function addFoodToCart($food_id,$shop_id,$uid,$num)
  {


          //http://localhost/CampusReservations/public/index.php/home/shop/addFoodToCart/food_id/5/shop_id/5/uid/2/num/1
          $data=array();
          if($num==1&&$num!=0)
          {
           $data['food_id']=$food_id;
           $data['shop_id']=$shop_id;
           $data['user_id']=$uid;
           $data['num']=$num;
           $data['shoptime']=date("Y-m-d h:i:sa");
           $re=\think\Db::name('shopcart')->insert($data);
           if ($re) {
             echo "增加1";
           }
           else{
             echo "0";
           }
          }
       else
       {
       $data['num']=$num;
       //$data['shoptime']=date("Y-m-d h:i:sa");
       $res= \think\Db::name('shopcart')->where("user_id='$uid' AND food_id='$food_id'")->setInc('num');
       //echo \think\Db::name('shopcart')->getLastSql();
       if ($res) {
             echo "修改+1";
           }
           else{
             echo "增加失败";
           }
       }
  }


  //从购物车里删除数据(id问题暂定，不确定返回的数据是不是ID号，并且不知道$re的值是不是常在)
  public function subFoodToCart($food_id,$uid,$num)
  {
    //http://localhost/CampusReservations/public/index.php/home/shop/subFoodToCart/food_id/10/uid/10/num/4
       $data=array();
         if($num==0)
         {

           $re=\think\Db::name('shopcart')->where("user_id='$uid' AND food_id='$food_id'")->delete();
           if ($re) {
             echo "1";
           }
           else{
             echo "0";
           }
         }

       else
       {
        $data['shoptime']=date("Y-m-d h:i:sa");
        $res= \think\Db::name('shopcart')->where("user_id='$uid' AND food_id='$food_id'")->setDec('num');
        if ($res) {
               echo "true";
             }
             else{
               echo "减少失败";
             }
         }
  }
    // 餐厅列表
    public function lists()
    {
        
        //销量最高
        $topList=\think\Db::name('dining')
        ->alias('d')
        ->join('cr_order o','d.shop_id=o.shop_id')
        ->field('d.shopname,d.shop_id,d.pic')
        ->group('o.shop_id')
        ->order('sum(o.num) desc')
        ->limit(5)
        ->select();
        //店铺列表
        $shopList=\think\Db::name('dining')->order('createtime desc')->paginate(6);
        $page=$shopList->render();
        //新店入住
        $newshop = \think\Db::name('dining')->order('createtime desc')->limit(5)->select();
        $this->assign("page",$page);
         $this->assign("newshop",$newshop);
        $this->assign("shopList",$shopList);
        $this->assign("topList",$topList);
        return $this->fetch();
    }
    

    // 餐厅详情
     public function detail($shop_id)
    {
        //得到店铺ID
        $id=$shop_id;
        //点击浏览店铺是点击率自增1
        \think\Db::name("dining")->where("shop_id='$id'")->setInc("obserave","1");
        //得到店铺详情
        $shopinfo= \think\Db::name('dining')->where("shop_id='$id'")->select();
        //得到评论列表   (注)：代码要修改，在显示有回复评论的同时，也要现在没有评论的
        $list=\think\Db::view('message','id,uid,did,mdate as messdate,message')
        ->view('reply','id,mid,mdate as rdate,message,uid,shopid','message.id=reply.mid')
        ->view('users','pic as upic,nickname','message.uid=users.id')
        ->view('dining','pic as dpic,shop_id,shopname',"dining.shop_id='$id'")
        ->where("message.did='$id'")
        ->order("message.mdate desc")
        ->select();
        //得到美食类别和类别下的美食
        $type=\think\Db::name('type')->where("shop_id='$id'")->select();
        //$num=\think\Db::name('type')->where("shop_id='$id'")->count();
        $count=0;
        $tfood=array();
        for ($i=0; $i <sizeof($type) ; $i++) { 
            $tid=$type[$i]['type_id'];
             /*$sql1="select * from cr_food left join  cr_type on cr_food.type_id=cr_type.type_id AND cr_food.shop_id=cr_type.shop_id where cr_type.type_id='$tid' limit 5";*/
             $sql1="select cr_food.shop_id,cr_food.food_id,cr_shopcart.num,cr_food.pic,cr_food.price,cr_food.intro,cr_food.foodname from cr_food left join  cr_type on cr_food.type_id=cr_type.type_id AND cr_food.shop_id=cr_type.shop_id left join cr_shopcart on cr_food.food_id=cr_shopcart.food_id where cr_type.type_id='$tid' ";
             $tfood[$i]=\think\Db::name('')->query($sql1);
           
        }
        $this->assign("tfood",$tfood);
        $this->assign("type",$type);
        $this->assign("plist",$list);
        $this->assign("shopinfo",$shopinfo);
       return $this->fetch();
    }


     //热门（人气）餐厅列表（按订单数降序排列）
     public function hotshoplists()
    {
        //0元配送（按配送费为0及订单数降序排列）
        $feeList=\think\Db::name('dining')
        ->alias('d')
        ->join('cr_order o','d.shop_id=o.shop_id')
        ->field('d.shopname,d.shop_id,d.pic')
        ->group('o.shop_id')
        ->order('sum(o.num) desc')
        ->where('d.fei=0')
        ->limit(5)
        ->select();
        //按评分排名
        $levelList=\think\Db::name('dining')->order('level desc')->limit(5)->select();
        //热门餐厅,无销量餐厅没有显示
        /*  $sql="select sum(o.num) s ,d.shopname, d.pic, d.hint,d.level,d.fee,d.shop_id from cr_dining d left join cr_order o on d.shop_id=o.shop_id  GROUP by o.shop_id order by s desc ";
        $list=\think\Db::name('')->query($sql);
        dump($list);*/
        $p=6;
        $list=\think\Db::name('dining')
        ->alias('d')
        ->join('cr_order o','d.shop_id=o.shop_id')
        ->field('d.shopname,d.level,d.obserave,d.shop_id,d.fei,d.pic,sum(o.num) s')
        ->group('o.shop_id')
        ->order('sum(o.num) desc')
        ->paginate($p);
        $page=$list->render();
        $this->assign('levelList',$levelList);  
        $this->assign('list',$list); 
        $this->assign('page',$page);   
        $this->assign('feeList',$feeList);   
        return $this->fetch();
    }


     // 新进餐厅列表（按时间降序排列）
     public function newshoplists()
     {

        //销量最高
        $topList=\think\Db::name('dining')
        ->alias('d')
        ->join('cr_order o','d.shop_id=o.shop_id')
        ->field('d.shopname,d.shop_id,d.pic')
        ->group('o.shop_id')
        ->order('sum(o.num) desc')
        ->limit(5)
        ->select();
        //最新餐厅列表
         $newshop = \think\Db::name('dining')->order('createtime desc')->paginate(6);
         //0元配送
         $zero=\think\Db::name('dining')->where("fei='0'")->order('createtime desc')->field('pic,shopname,shop_id')->limit(5)->select();
         $this->assign('zero',$zero);
         $this->assign('topList',$topList);
         $this->assign('newshop',$newshop);
         return $this->fetch();
    }

    public  function  order(){
       $user = session('id');//获取用户ID
      if (request()->isPost()){
      }
    }
}
